﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class active_WithMe_admin_Friend : System.Web.UI.Page
{
    protected XmlNodeList xnl = null;
    protected string message = string.Empty;
    private static string xmlName = "../boyAndGirl.xml";
    private static string xmlNodeName = "User";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null || Session["user"].ToString().Length <= 0)
        {
            Response.Redirect("login.aspx");
            Response.End();
        }
        if (Request.QueryString["delete"] != null && Request.QueryString["delete"].ToString() == "yes")
        {

            DeleteContext();
        }
        if (Session["message"] != null && Session["message"].ToString().Length > 0)
        {
            message = Session["message"].ToString();
            Session["message"] = "";
        }
        init();

    }


    private void init()
    {

        XmlDocument xml = LoadXml(xmlName);
        XmlNode xn = null;
        xn = xml.SelectSingleNode(xmlNodeName);
        if (xn != null)
            xnl = xn.ChildNodes;

    }

    protected void SignOut(object sender, EventArgs e)
    {
        Session["user"] = "";
        Response.Redirect("login.aspx");
    }



    //load xml file
    private XmlDocument LoadXml(string FileName)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath(FileName));
        return xmlDoc;
    }




    public void EditContent(object sender, EventArgs e)
    {
        if (Request.Form["name"] == null || Request.Form["date-input"] == null || Request.Form["BoyName-input"] == null || Request.Form["GirlName-input"] == null || Request.Form["BoyImages"] == null || Request.Form["GirlImages"] == null || Request.Form["State-input"] == null ||
             Request.Form["date-input"].ToString().Length <= 0 || Request.Form["BoyName-input"].ToString().Length <= 0 || Request.Form["GirlName-input"].ToString().Length <= 0 || Request.Form["BoyImages"].ToString().Length <= 0 || Request.Form["GirlImages"].ToString().Length <= 0 ||
                Request.Form["State-input"].ToString().Length <= 0
            )
        {
            Session["message"] = "NoComplete";
            Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
            return;
        }
        try
        {
            XmlDocument xml = LoadXml(xmlName);
            XmlNode xn = xml.SelectSingleNode(xmlNodeName);
            if (xn != null)
                xnl = xn.ChildNodes;
            if (xnl == null)
            {
                return;
            }
            if (Request.Form["name"].ToString().Length > 0)
            {
                foreach (XmlNode xf in xnl)
                {

                    XmlElement xe = (XmlElement)xf;

                    if (xe.GetAttribute("name") == Request.Form["name"].ToString())
                    {

                        xe.SetAttribute("date", Request.Form["date-input"].ToString());
                        xe.SetAttribute("BoyName", Request.Form["BoyName-input"].ToString());
                        xe.SetAttribute("GirlName", Request.Form["GirlName-input"].ToString());
                        xe.SetAttribute("BoyImage", Request.Form["BoyImages"].ToString().Replace("../", ""));
                        xe.SetAttribute("GirlImage", Request.Form["GirlImages"].ToString().Replace("../", ""));
                        xe.SetAttribute("State", Request.Form["State-input"].ToString());
                        break;

                    }

                }
            }
            else
            {
                XmlElement xe = xml.CreateElement("ActiveInfo");

                
                long i = 0;
                if (xnl.Count>0)
                {
                     i = long.Parse(xnl[xnl.Count - 1].Attributes["name"].Value) + 1;
                }
                else
                {
                    i = 1;
                }
                xe.SetAttribute("name", i.ToString());
                xe.SetAttribute("date", Request.Form["date-input"].ToString());
                xe.SetAttribute("BoyName", Request.Form["BoyName-input"].ToString());
                xe.SetAttribute("GirlName", Request.Form["GirlName-input"].ToString());
                xe.SetAttribute("BoyImage", Request.Form["BoyImages"].ToString().Replace("../", ""));
                xe.SetAttribute("GirlImage", Request.Form["GirlImages"].ToString().Replace("../", ""));
                xe.SetAttribute("State", Request.Form["State-input"].ToString());
                xn.AppendChild(xe);
            }

            xml.Save(Server.MapPath(xmlName));

        }
        catch
        {
            Session["message"] = "error";
            Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
            return;
        }
        Session["message"] = "success";
        Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);


    }

    private void DeleteContext()
    {
        if (Request.QueryString["id"] == null || Request.QueryString["id"].ToString().Length <= 0)
        {

            return;
        }
        try
        {
            XmlDocument xml = LoadXml(xmlName);
            XmlNode xn = xml.SelectSingleNode(xmlNodeName);
            if (xn != null)
                xnl = xn.ChildNodes;
            if (xnl == null)
            {
                return;
            }

            if (Request.QueryString["id"].ToString().Length > 0)
            {
                foreach (XmlNode xf in xnl)
                {

                    XmlElement xe = (XmlElement)xf;

                    if (xe.GetAttribute("name") == Request.QueryString["id"].ToString())
                    {

                        xn.RemoveChild(xf);

                        break;

                    }

                }

                xml.Save(Server.MapPath(xmlName));
            
            }
        }
        catch
        {
            Session["message"] = "error";
            Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
            return;
        }
        Session["message"] = "success";
        Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);


    }


}
